package com.ruoyi.work.service.impl;

import java.beans.Transient;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.work.domain.WorkExpense;
import com.ruoyi.work.domain.WorkInvoice;
import com.ruoyi.work.domain.WorkPaper;
import com.ruoyi.work.mapper.WorkExpenseMapper;
import com.ruoyi.work.mapper.WorkInvoiceMapper;
import com.ruoyi.work.mapper.WorkPaperMapper;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.work.mapper.WorkIncomingMapper;
import com.ruoyi.work.domain.WorkIncoming;
import com.ruoyi.work.service.IWorkIncomingService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收款认领Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-21
 */
@Service
public class WorkIncomingServiceImpl implements IWorkIncomingService {
    @Autowired
    private WorkIncomingMapper workIncomingMapper;
    @Autowired
    private WorkExpenseMapper workExpenseMapper;
    @Autowired
    private WorkInvoiceMapper workInvoiceMapper;
    @Autowired
    private WorkPaperMapper workPaperMapper;

    /**
     * 查询收款认领
     *
     * @param incomingId 收款认领主键
     * @return 收款认领
     */
    @Override
    public WorkIncoming selectWorkIncomingByIncomingId(Long incomingId) {

        return workIncomingMapper.selectWorkIncomingByIncomingId(incomingId);
    }
    /**
     * 查询收款认领
     *
     * @param incomingIds 收款认领主键
     * @return 收款认领
     */
    @Override
    public List<WorkIncoming> selectWorkIncomingByIncomingIds(String[] incomingIds) {

        return workIncomingMapper.selectWorkIncomingByIncomingIds(incomingIds);
    }
    /**
     * 查询收款认领列表
     *
     * @param workIncoming 收款认领
     * @return 收款认领
     */
    @Override
    public List<WorkIncoming> selectWorkIncomingList(WorkIncoming workIncoming) {
        return workIncomingMapper.selectWorkIncomingList(workIncoming);
    }

    /**
     * 新增收款认领
     *
     * @param workIncoming 收款认领
     * @return 结果
     */
    @Override
    public int insertWorkIncoming(WorkIncoming workIncoming) {
        workIncoming.setCreateTime(DateUtils.getNowDate());
        return workIncomingMapper.insertWorkIncoming(workIncoming);
    }

    /**
     * 修改收款认领
     *
     * @param workIncoming 收款认领
     * @return 结果
     */
    @Override
    public int updateWorkIncoming(WorkIncoming workIncoming) {
        workIncoming.setUpdateTime(DateUtils.getNowDate());
        return workIncomingMapper.updateWorkIncoming(workIncoming);
    }

    /**
     * 批量删除收款认领
     *
     * @param incomingIds 需要删除的收款认领主键
     * @return 结果
     */
    @Override
    public int deleteWorkIncomingByIncomingIds(String incomingIds) {
        return workIncomingMapper.deleteWorkIncomingByIncomingIds(Convert.toStrArray(incomingIds));
    }

    /**
     * 删除收款认领信息
     *
     * @param incomingId 收款认领主键
     * @return 结果
     */
    @Override
    public int deleteWorkIncomingByIncomingId(Long incomingId) {
        return workIncomingMapper.deleteWorkIncomingByIncomingId(incomingId);
    }

    @Transactional
    @Override
    public int insertCancel(WorkIncoming workIncoming) {
        if (StringUtils.isNotEmpty(workIncoming.getParams().get("taxs").toString()))
            workIncoming.setTaxs(Convert.toStrArray(workIncoming.getParams().get("taxs").toString()));//税点
        if (StringUtils.isNotEmpty(workIncoming.getParams().get("expenses").toString()))
            workIncoming.setExpenses(Convert.toStrArray(workIncoming.getParams().get("expenses").toString()));//返款单
        if (StringUtils.isNotEmpty(workIncoming.getParams().get("invoices").toString()))
            workIncoming.setInvoices(Convert.toStrArray(workIncoming.getParams().get("invoices").toString()));//发票
        if (StringUtils.isNotEmpty(workIncoming.getParams().get("papers").toString()))
            workIncoming.setPapers(Convert.toStrArray(workIncoming.getParams().get("papers").toString()));//稿件
        workIncomingMapper.insertCancel(workIncoming);

        if (StringUtils.isNotEmpty(workIncoming.getParams().get("taxs").toString()))
            workIncoming.setTaxes(workExpenseMapper.selectWorkExpenseByExpenseIds(Convert.toStrArray(workIncoming.getParams().get("taxs").toString())).stream().map(WorkExpense::getExpenseFee).reduce(BigDecimal.ZERO, BigDecimal::add));
        if (StringUtils.isNotEmpty(workIncoming.getParams().get("expenses").toString()))
            workIncoming.setRefund(workExpenseMapper.selectWorkExpenseByExpenseIds(Convert.toStrArray(workIncoming.getParams().get("expenses").toString())).stream().map(WorkExpense::getExpenseFee).reduce(BigDecimal.ZERO, BigDecimal::add));
        if (StringUtils.isNotEmpty(workIncoming.getParams().get("invoices").toString())) {

            //已销额度
            workIncoming.setExpenseLimit(workInvoiceMapper.selectWorkInvoiceByInvoiceIds(Convert.toStrArray(workIncoming.getParams().get("invoices").toString())).stream().map(WorkInvoice::getInvoicePrice).reduce(BigDecimal.ZERO, BigDecimal::add));

            //根据发票ID，更新稿件销账时间
            workPaperMapper.updateWorkPaperCancelTime(Convert.toStrArray(workIncoming.getParams().get("invoices").toString()));

        }

        if (StringUtils.isNotEmpty(workIncoming.getParams().get("papers").toString())) {
            workIncoming.setExpenseLimit(workPaperMapper.selectWorkPaperByPaperIds(Convert.toStrArray(workIncoming.getParams().get("papers").toString())).stream().map(WorkPaper::getActualPrice).reduce(BigDecimal.ZERO, BigDecimal::add));
            workPaperMapper.selectWorkPaperByPaperIds(Convert.toStrArray(workIncoming.getParams().get("papers").toString())).forEach(w -> {
                w.setOffAccountDate(DateUtils.getNowDate());//根据稿件ID，更新稿件销账时间
                workPaperMapper.updateWorkPaper(w);
            });
        }
        return workIncomingMapper.updateWorkIncoming(workIncoming);
    }

    //更新提成
    public int updateWorkCommission(Long incomingId){
        return workIncomingMapper.updateWorkCommission(incomingId);
    }
}
